﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Threading;

namespace ConnChecker
{
    public partial class Form1 : Form
    {
        Thread pingert;
        string pingstatus;
        string roundtrip;
        string target;


        public Form1()
        {
            InitializeComponent();
            pingert = new Thread(new ThreadStart(pinger));
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            notifyIcon1.Icon = ConnChecker.Properties.Resources.Connected1;
            notifyIcon1.Text = "ConnChecker";

            this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
        }

        void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();

        }

        public void pinger()
        {
            System.Net.NetworkInformation.Ping ping1 = new System.Net.NetworkInformation.Ping();

            while (true)
            {
                try
                {
                    roundtrip = ping1.Send(target, 1000).RoundtripTime.ToString();
                    pingstatus = ping1.Send(target, 1000).Status.ToString();
                }
                catch (Exception)
                {
                    pingstatus = "FAILED";
                }

                Thread.Sleep(1000);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "yiff")
            {
                MessageBox.Show("RAWR YEA HUN");
                return;
            }

            timer1.Start();
           
            target = textBox1.Text;
            if (pingert.IsAlive == false)
            {
                pingert.Start();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = "Connection test: " + pingstatus;
            label2.Text = "Last roundtrip time: " + roundtrip + "ms" ;

            if (Convert.ToInt16(roundtrip) < 70)
            {
                notifyIcon1.Icon = ConnChecker.Properties.Resources.Connected1;

            }
            else if (pingstatus == "FAILED")
            {
                notifyIcon1.Icon = ConnChecker.Properties.Resources.Error;
            }
            else if (Convert.ToInt16(roundtrip) > 70)
            {
                notifyIcon1.Icon = ConnChecker.Properties.Resources.Slow;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            notifyIcon1.Dispose();
            Environment.Exit(0);
        }

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            notifyIcon1.ShowBalloonTip(10000, "Info", "Connection to host " + target + ": " + pingstatus + "\n Last roundtrip time was: " + roundtrip + "ms.",ToolTipIcon.Info);
        }

        private void notifyIcon1_BalloonTipClicked(object sender, EventArgs e)
        {
            this.Show();
        }

      
    }
}
